package ie.dkit.semanticPhotoWeb.client.panels.profile;

import ie.dkit.semanticPhotoWeb.client.dtos.PersonDTO;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;

import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.MessageBox.ConfirmCallback;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.HorizontalLayout;

public class KnowsPanel extends Panel {

	private EditUserProfileWindow m_EditUserProfileWindow;

	protected KnowsPanel(EditUserProfileWindow editUserProfileWindow) {
		this.m_EditUserProfileWindow = editUserProfileWindow;
		setCollapsible(true);
		setTitle("Known Persons");
	}

	void addPersons(PersonDTO[] persons) {
		for (int i = 0; i < persons.length; i++) {
			addPerson(persons[i]);
		}
		doLayout();
	}

	void addPerson(final PersonDTO person) {
		final Panel panel = new Panel();
		panel.setLayout(new HorizontalLayout(1));
		panel.add(new HTML("Name: " + person.name + "; Username: "
				+ person.username));
		panel.add(new Button("X", new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				MessageBox
						.confirm(
								"Delete",
								"Are you sure you want to delete the Relation to this Person?",
								new ConfirmCallback() {
									public void execute(String btnID) {
										if (btnID.equals("yes")) {
											m_EditUserProfileWindow.m_ClientServerConnectorAsync
													.deleteKnownRelation(
															person,
															new AsyncCallbackAdapter() {
																public void onSuccess(
																		Object result) {
																	remove(panel);
																	doLayout();
																}
															});
										}
									}
								});
			}
		}));
		add(panel);
	}
}
